//
//  WBVisitorView.swift
//  TYCWeiBo
//
//  Created by 中发 on 2018/6/1.
//  Copyright © 2018年 中发. All rights reserved.
//

import UIKit

/// 访客视图
class WBVisitorView: UIView {

    //注册按钮
    let registerBtn: UIButton = UIButton.init(title: "注册", fontSize: 16, normalColor: UIColor.orange, highlightedColor: UIColor.darkGray, backgroundImageName: "common_button_white_disable")
    //登陆按钮
    let loginBtn: UIButton = UIButton.init(title: "登陆", fontSize: 16, normalColor: UIColor.darkGray, highlightedColor: UIColor.darkGray, backgroundImageName: "common_button_white_disable")
    
    /// 访客视图信息字典
    //如果是首页 imageName = ""
    var visitorInfo: [String: String]? {
        didSet{
            //取字典信息
            guard let imageName = visitorInfo?["imageName"], let message = visitorInfo?["message"] else {
                return
            }
            //设置提示信息
            tipLabel.text = message
            //设置图像，首页不需要设置
            if imageName == "" {
                //开始动画
                startAnimation()
                return
            }
            iconView.image = UIImage(named: imageName)
            //其他控制器的访客视图不需要显示小房子/遮罩视图
            houseIconView.isHidden = true
            maskIconView.isHidden = true
        }
    }
    
    //MAEK:--构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置首页转轮动画（首页）
    private func startAnimation() {
        let anim = CABasicAnimation.init(keyPath: "transform.rotation")
        //'M_PI' is deprecated: Please use 'Double.pi' or '.pi' to get the value of correct type and avoid casting.
        anim.toValue = 2 * Double.pi
        anim.duration = 15
        anim.repeatCount = MAXFLOAT
        //动画完成不删除，如果iconView被释放，动画会一起被销毁
        //在设置连续播放的动画中非常有效
        anim.isRemovedOnCompletion = false
        //将动画添加到图层
        iconView.layer.add(anim, forKey: nil)
    }
    
    ///创建私有控件
    //懒加载属性只有调用UIKit控件指定的构造函数，其他都需要使用类型
    //图像视图
    private let iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //遮罩视图
    private let maskIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //小房子
    private let houseIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //提示标签
    private let tipLabel: UILabel = UILabel.init(title: "关注一些人，会这里看看有什么惊喜关注一些人。回这里看看有什么惊喜", fontSize: 14, color: UIColor.darkGray, screenInset: 0)
    
}

// MARK: - 设置界面
extension WBVisitorView {

    func setUpUI() {
        backgroundColor = #colorLiteral(red: 0.9294117647, green: 0.9294117647, blue: 0.9294117647, alpha: 1)
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(houseIconView)
        addSubview(tipLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        //设置提示label文字居中
        tipLabel.textAlignment = .center
        //2.取消Autoresizing
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        //3.自动布局
        let margen: CGFloat = 20.0
        
        //图像视图
        addConstraint(NSLayoutConstraint.init(item: iconView,
                                              attribute: .centerX,
                                              relatedBy: .equal, toItem: self,
                                              attribute: .centerX,
                                              multiplier: 1.0,
                                              constant: 0))
        addConstraint(NSLayoutConstraint.init(item: iconView,
                                              attribute: .centerY,
                                              relatedBy: .equal,
                                              toItem: self,
                                              attribute: .centerY,
                                              multiplier: 1.0,
                                              constant: -60))
        //小房子
        addConstraint(NSLayoutConstraint.init(item: houseIconView,
                                              attribute: .centerX,
                                              relatedBy: .equal,
                                              toItem: iconView,
                                              attribute: .centerX,
                                              multiplier: 1.0,
                                              constant: 0))
        addConstraint(NSLayoutConstraint.init(item: houseIconView,
                                              attribute: .centerY,
                                              relatedBy: .equal,
                                              toItem: iconView,
                                              attribute: .centerY,
                                              multiplier: 1.0,
                                              constant: 0))
        //提示Label
        addConstraint(NSLayoutConstraint.init(item: tipLabel,
                                              attribute: .centerX,
                                              relatedBy: .equal,
                                              toItem: iconView,
                                              attribute: .centerX,
                                              multiplier: 1.0,
                                              constant: 0))
        addConstraint(NSLayoutConstraint.init(item: tipLabel,
                                              attribute: .top,
                                              relatedBy: .equal,
                                              toItem: iconView,
                                              attribute: .bottom,
                                              multiplier: 1.0,
                                              constant: margen))
        addConstraint(NSLayoutConstraint.init(item: tipLabel,
                                              attribute: .width,
                                              relatedBy: .equal,
                                              toItem: nil,
                                              attribute: .notAnAttribute,
                                              multiplier: 1.0,
                                              constant: 236))
        //注册按钮
        addConstraint(NSLayoutConstraint.init(item: registerBtn,
                                              attribute: .left,
                                              relatedBy: .equal,
                                              toItem: tipLabel,
                                              attribute: .left,
                                              multiplier: 1.0,
                                              constant: 0))
        addConstraint(NSLayoutConstraint.init(item: registerBtn,
                                              attribute: .top,
                                              relatedBy: .equal,
                                              toItem: tipLabel,
                                              attribute: .bottom,
                                              multiplier: 1.0,
                                              constant: margen))
        addConstraint(NSLayoutConstraint.init(item: registerBtn,
                                              attribute: .width,
                                              relatedBy: .equal,
                                              toItem: nil,
                                              attribute: .notAnAttribute,
                                              multiplier: 1.0,
                                              constant: 100))
        //登录按钮
        addConstraint(NSLayoutConstraint.init(item: loginBtn,
                                              attribute: .right,
                                              relatedBy: .equal,
                                              toItem: tipLabel,
                                              attribute: .right,
                                              multiplier: 1.0,
                                              constant: 0))
        addConstraint(NSLayoutConstraint.init(item: loginBtn,
                                              attribute: .top,
                                              relatedBy: .equal,
                                              toItem: tipLabel,
                                              attribute: .bottom,
                                              multiplier: 1.0,
                                              constant: margen))
        addConstraint(NSLayoutConstraint.init(item: loginBtn,
                                              attribute: .width,
                                              relatedBy: .equal,
                                              toItem: registerBtn,
                                              attribute: .width,
                                              multiplier: 1.0,
                                              constant: 0))
        //遮罩视图
        //views: 定义VFL中控件名称和实际名称间的映射关系
        //metrics: 定义VFL中（）指定的常数映射关系
        let viewDic = ["maskIconView": maskIconView,"registerBtn": registerBtn]
        let metrics = ["spacing": -35]
        
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[maskIconView]-0-|", options: [], metrics: nil, views: viewDic))
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[maskIconView]-(spacing)-[registerBtn]", options: [], metrics: metrics, views: viewDic))
    }
    
}
